package com.example.fmmall.controller;

import com.example.fmmall.service.UserService;
import com.example.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Date:2022/1/27
 * Author:Sun
 * Description:
 * Note:
 */
//@Controller
//@ResponseBody
@RestController
@RequestMapping("/user")
//@Api 类注解，在控制器添加该注解，可以控制器类进行功能说明
@Api(value = "提供用户的登录和注册接口", tags = "用户管理")
//允许跨域。实际上是设置响应头允许跨域。
@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;

    //响应数据说明
    //url http://ip:port/fmmall/user/login
    //params 参数类型，参数个数
    //method GET 请求方式

    //ApiOperation 方法注解，对接口方法说明
    @ApiOperation("用户登录接口")
    //ApiImplicitParams 参数注解，对参数说明
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "用户登录密码", required = true)
    })
    //接收响应
    //@RequestMapping(value = "/login", method = RequestMethod.GET)
    @GetMapping("/login")
    //key: username必须传值，password有默认值111;
    public ResultVO login(@RequestParam("username") String name, @RequestParam(value = "password") String pwd) {
        ResultVO resultVO = userService.checkLogin(name, pwd);
        return resultVO;
    }

    @ApiOperation("用户注册接口")
    //@RequestMapping(value = "regist", method = RequestMethod.POST)
    //@ApiImplicitParam(name = "用户信息",required = true).在User类加了类注解这句就可以不写。
    //public ResultVO regist(User user)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户注册帐号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "用户注册密码", required = true)
    })
    @PostMapping("/regist")
    public ResultVO regist(String username, String password) {
        ResultVO resultVO = userService.userResgist(username, password);
        return resultVO;
    }
}




